package array.easy;

import java.util.Arrays;

/**
 * 35. 搜索插入位置
 * @author wangtianqi
 */
public class searchInsert {

    public static void main(String[] args) {

        int[] test = {0, 1, 3, 4, 5, 6, 7, 8};
        System.out.println(myCode(test, 1));
    }

    public static int myCode(int[] nums, int target) {

        int left = 0;
        int right = nums.length - 1;

        while (left <= right) {
            int middle = ((right - left) / 2) + left;
            if (target < nums[middle]) {
                right = middle - 1;
            } else if (target > nums[middle]) {
                left = middle + 1;
            } else if (target == nums[middle]) {
                return middle;
            }
        }

        return left;
    }
}
